package main

import (
	"fmt"
)

func main() {
	res := reconstructMatrix(9, 2, []int{0,1,2,0,0,0,0,0,2,1,2,1,2})
	fmt.Println(res)
}

func reconstructMatrix(upper int, lower int, colsum []int) [][]int {
	upperBackup := upper
	lowerBackup := lower
	var count int = 0
	list := [][]int{}
	for _,n := range colsum{
		count += n
	}

	if count != upper + lower {
		return list
	}


	for i := 0; i < 2; i++ {
		list = append(list, make([]int, len(colsum)))
	}

	for i,num := range colsum{
		if num == 2{
			list[0][i] = 1
			list[1][i] = 1
			upper --
			lower --
		}
		if num == 0{
			list[0][i] = 0
			list[1][i] = 0
		}
		if num == 1{
			if upper - lower >0{

				list[0][i] = 1
				list[1][i] = 0
				upper --
			}else {
				list[0][i] = 0
				list[1][i] = 1
				lower --
			}
		}

	}

	up := 0
	low := 0

	for _,n := range list[0]{

		up += n
	}

	for _,m := range list[1]{

		low += m
	}

	if up + low == count && up == upperBackup && low == lowerBackup{
		return list
	}else {
		return [][]int{}
	}

	return list
}
